home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 353_02 / objlist.cpp < prev    next >
C/C++ Source or Header  |  1992-01-18  |  2KB  |  84 lines

  1.                                  // Chapter 6 - Program 5
  2. #include <iostream.h>
  3.  
  4. class box {
  5.    int length;
  6.    int width;
  7.    box *another_box;
  8. public:
  9.    box(void);             //Constructor
  10.    void set(int new_length, int new_width);
  11.    int get_area(void);
  12.    void point_at_next(box *where_to_point);
  13.    box *get_next(void);
  14. };
  15.  
  16.  
  17. box::box(void)        //Constructor implementation
  18. {
  19.    length = 8;
  20.    width = 8;
  21.    another_box = NULL;
  22. }
  23.  
  24.  
  25. // This method will set a box size to the two input parameters
  26. void box::set(int new_length, int new_width)
  27. {
  28.    length = new_length;
  29.    width = new_width;
  30. }
  31.  
  32.  
  33. // This method will calculate and return the area of a box instance
  34. int box::get_area(void)
  35. {
  36.    return (length * width);
  37. }
  38.  
  39.  
  40. // This method causes the pointer to point to the input parameter
  41. void box::point_at_next(box *where_to_point)
  42. {
  43.    another_box = where_to_point;
  44. }
  45.  
  46.  
  47. // This method returns the box the current one points to
  48. box *box::get_next(void)
  49. {
  50.    return another_box;
  51. }
  52.  
  53.  
  54. main()
  55. {
  56. box small, medium, large;          //Three boxes to work with
  57. box *box_pointer;                      //A pointer to a box
  58.  
  59.    small.set(5, 7);
  60.    large.set(15, 20);
  61.    
  62.    cout << "The small box area is " << small.get_area() << "\n";
  63.    cout << "The medium box area is " << medium.get_area() << "\n";
  64.    cout << "The large box area is " << large.get_area() << "\n";
  65.  
  66.    small.point_at_next(&medium);
  67.    medium.point_at_next(&large);
  68.  
  69.    box_pointer = &small;
  70.    box_pointer = box_pointer->get_next();
  71.    cout << "The box pointed to has area " << 
  72.                                   box_pointer->get_area() << "\n";
  73. }
  74.  
  75.  
  76.  
  77.  
  78. // Result of execution
  79. //
  80. // The small box area is 35
  81. // The medium box area is 64
  82. // The large box area is 300
  83. // The box pointed to has area 64
  84.